<?php
	error_reporting(~E_ERROR);

	define('ROOT',dirname(__FILE__).'/');

	$delIds[] = $_POST['delIds'];
//	var_dump($delIds);
	$delArr[] = explode(",", $delIds[0]);
/*	$data=file_get_contents('php://input');
	if($data)
	{		
		$jsondata=json_decode($data,true);
	}
	$delArr[] = $jsondata['delIds'];*/
	$myconn=dwopendb();
	$i=0;
	foreach($delArr[0] as $ide)
	{ 
//		var_dump($delArr);
//		file_put_contents("test.log","ide: ".$ide." LINE:".__LINE__."\n", FILE_APPEND);
//		foreach($ide as $id)
//		{
			$exec="SELECT staffnum FROM staffbasesetting where rowid=$ide AND rowid!=1;";
			$result=mysql_query($exec,$myconn);
			$gonghaoji = mysql_fetch_array($result) ;
			file_put_contents("test.log","sql: ".$exec."  gonghaoji[0]: ".$gonghaoji[0]." LINE:".__LINE__."\n", FILE_APPEND);
			if($gonghaoji[0])
			{
				$destination_folder =ROOT."tupian/".$gonghaoji[0];
				file_put_contents("test.log","delete: ".$destination_folder." LINE:".__LINE__."\n", FILE_APPEND);
				delDirAndFile($destination_folder,true);
				$destination_folder =ROOT."../biduilujing/".$gonghaoji[0].".dat";
				file_put_contents("test.log","delete: ".$destination_folder." LINE:".__LINE__."\n", FILE_APPEND);
				delDirAndFile($destination_folder,false);
			}

			$exec="delete from staffbasesetting where rowid=$ide AND rowid!=1;";
			$result=mysql_query($exec,$myconn); 
			file_put_contents("test.log","exec: ".$exec." LINE:".__LINE__."\n", FILE_APPEND);
			if((mysql_affected_rows($myconn)==0) or (mysql_affected_rows($myconn)==-1)) 
			{ 
				echo '{"errorMsg":"没有找到记录，或者删除时出错"}';
	//			echo "<script language=\"JavaScript\">alert(\"没有找到记录，或者删除时出错\");</script><br/>"; 
		//				echo "<script language=\"JavaScript\">self.setTimeout(window.location.href='/door/adminsetting.php',5000);</script><br/>";
		//				exit;
				mysql_free_result($result);
				dwclosedb($myconn);
				return; 
			} 
			else
			{ 			
				$i += 1;
		//		echo "<script language=\"JavaScript\">alert(\"序号 ".$ide." 已删除。\");</script><br/>"; 
		/*				echo "<script language=\"JavaScript\">alert('授权成功。');</script><br/>";
				echo "<script language=\"JavaScript\">alert('序号 ".$ide." 已删除。');</script><br/>"; 
				echo "<script language=\"JavaScript\">self.setTimeout(window.location.href='/door/adminsetting.php',5000);</script><br/>";
		*/
		//		echo '{"res":"fail"}';	
			}
			mysql_free_result($result);			
//		}
	}	
	dwclosedb($myconn);
	echo '{"success":"true","delNums":"'.$i.'"}';
	return;

	function dwclosedb($myconn)
	{
		mysql_close($myconn);	
	}
	
	function dwopendb()
	{
		$myconn=@mysql_connect("localhost","dw","DWdawu-309");
		if(!$myconn)
		{
//			die('Could not connect: '.mysql_error());
			$string = "Can't connect database : ".mysql_error()." LINE:".__LINE__."\n";
			file_put_contents("test.log", $string, FILE_APPEND);
			echo '{"errorMsg":$string}';
			return false;
		}
		$db_selected=mysql_select_db("dwzuapairenlian",$myconn);
		if(!$db_selected)
		{
//			die("Can't use database dwaccessDB : ".mysql_error());       
			mysql_close($myconn);
			$string = "Can't selected database : ".mysql_error()." LINE:".__LINE__."\n";
			file_put_contents("test.log", $string, FILE_APPEND);
			return false;
		}
		return $myconn;
	}	

	 //循环删除目录和文件函数
	function delDirAndFile($path, $delDir ) 
    {
	    file_put_contents("test.log", "\n path: ".$path." LINE: ".__LINE__ ."\n", FILE_APPEND);
	    if (is_array($path)) 
	    {
	        foreach ($path as $subPath)
	            delDirAndFile($subPath, $delDir);
	    }
	    if (is_dir($path)) 
	    {
	        $handle = opendir($path);
	        if ($handle) 
	        {
	            while (false !== ( $item = readdir($handle) )) 
	            {
	                if ($item != "." && $item != "..")
	                    is_dir("$path/$item") ? delDirAndFile("$path/$item", $delDir) : unlink("$path/$item");
	            }
	            closedir($handle);
	            if ($delDir)
	                return rmdir($path);
	        }
	    } else 
	    {
	        if (file_exists($path))
	         {
	            return unlink($path);
	        } else 
	        {
	            return FALSE;
	        }
	    }
	    clearstatcache();
	}

?>	